package com.lrd.snapshotorder.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.lrd.snapshotorder.entity.SnapshotOrder;
import com.lrd.snapshotorder.mapper.SnapshotOrderMapper;
import com.lrd.snapshotorder.protocol.ResultData;
import com.lrd.snapshotorder.protocol.dto.order.SnapshotOrderListDto;
import com.lrd.snapshotorder.protocol.dto.order.SnapshotOrderQueryDto;
import com.lrd.snapshotorder.service.SnapshotOrderService;
import com.lrd.snapshotorder.util.ExportToExcel;
import lombok.extern.slf4j.Slf4j;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletResponse;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

@Service
@Slf4j
public class SnapshotOrderServiceImpl implements SnapshotOrderService {

    @Autowired
    private SnapshotOrderMapper snapshotOrderMapper;

    @Override
    public ResultData<List<SnapshotOrderListDto>> snapshotOrderPageList(SnapshotOrderQueryDto queryDto) {
        Page<SnapshotOrder> snapshotOrderPage = snapshotOrderMapper.snapshotOrderPageList(queryDto);
        if (CollUtil.isEmpty(snapshotOrderPage.getRecords())) {
            return new ResultData<>(0, "订单列表为空", new ArrayList<>(), 0L);
        }
        List<SnapshotOrderListDto> snapshotOrderListDtos = BeanUtil.copyToList(snapshotOrderPage.getRecords(), SnapshotOrderListDto.class);
        return new ResultData<>(0, "", snapshotOrderListDtos, snapshotOrderPage.getTotal());
    }

    @Override
    public ResultData<List<SnapshotOrder>> snapshotOrderAllList(SnapshotOrderQueryDto queryDto) {
        List<SnapshotOrder> allList = snapshotOrderMapper.snapshotOrderAllList(queryDto);
        return ResultData.success(allList);
    }

    @Override
    public void exportOrder(SnapshotOrderQueryDto queryDto, HttpServletResponse response) throws Exception {
        response.setContentType("application/vnd.ms-excel");
        response.setHeader("Connection", "close");
        response.setHeader("Content-disposition", "attachment; filename=" + new String((queryDto.getBatchNumber().toString()).getBytes("gb2312"), "iso8859-1") + ".xlsx");
        Workbook workbook = new XSSFWorkbook();
        Sheet sheet = workbook.createSheet("Sheet1");
        OutputStream outputStream = response.getOutputStream();
        ExportToExcel exportToExcel = new ExportToExcel(workbook, sheet, outputStream);
        Map<String, String> headersMap = new LinkedHashMap<>();
        headersMap.put("序号（必填）", null);
        headersMap.put("邮件号/订单号（必填）", "getOrderNo");
        headersMap.put("客户交寄时间（必填）", "getPostTime");
        headersMap.put("寄件人姓名", "getSender");
        headersMap.put("寄件人地址", "getSenderAddress");
        headersMap.put("收件人姓名", "getRecipient");
        headersMap.put("收件人地址", "getRecipientAddress");
        headersMap.put("邮费（必填）", "getPostage");
        headersMap.put("重量（克）", "getWeight");
        headersMap.put("备注", "getRemarks");
        //添加表头
        int cellNum = 0;
        Row row = sheet.createRow(0);
        for (Map.Entry<String, String> entry : headersMap.entrySet()) {
            Cell cell = row.createCell(cellNum++);
            cell.setCellValue(entry.getKey());
        }
        // 查询数据
        List<SnapshotOrder> allList = snapshotOrderMapper.snapshotOrderAllList(queryDto);
        int rowNumber = exportToExcel.exportToExcel(allList, headersMap, 1);
        log.info("exportOrder,导出数据总条数={}", rowNumber);
        outputStream.close();
        workbook.close();
    }

    @Override
    public List<SnapshotOrderListDto> getOrderByBatchNumber(Integer batchNumber) {
        SnapshotOrderQueryDto queryDto = new SnapshotOrderQueryDto();
        queryDto.setBatchNumber(batchNumber);
        List<SnapshotOrder> allList = snapshotOrderMapper.snapshotOrderAllList(queryDto);
        return BeanUtil.copyToList(allList, SnapshotOrderListDto.class);
    }

}
